package com.aspire.boc.mms.wmf.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPUtils {
	private static Log logger = LogFactory.getLog(FTPUtils.class);

	/**
	 * Description: 向FTP服务器上传文件
	 * 
	 * @param url
	 *            FTP服务器hostname
	 * @param port
	 *            FTP服务器端口
	 * @param username
	 *            FTP登录账号
	 * @param password
	 *            FTP登录密码
	 * @param path
	 *            FTP服务器保存目录
	 * @param filename
	 *            上传到FTP服务器上的文件名
	 * @param input
	 *            输入流
	 * @return 成功返回true，否则返回false
	 */
	public static boolean uploadFile(String url, int port, String username,
			String password, String path, String filename, InputStream input,
			int connectTimeout, int dataTimeout) throws Exception {
		boolean success = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.setConnectTimeout(connectTimeout);

			ftp.connect(url, port);// 连接FTP服务器
			logger.debug("Connected FTP");

			// 如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
			ftp.login(username, password);// 登录
			logger.debug("Logined FTP");

			ftp.enterLocalPassiveMode();
			ftp.setDataTimeout(dataTimeout);

			// 上传zip等binary需设置如下类型
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

			reply = ftp.getReplyCode();
			logger.info("FTP replyCode is " + reply);

			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return success;
			}
			ftp.changeWorkingDirectory(path);
			logger.debug("Changed workingDirectory");

			long start = System.currentTimeMillis();
			ftp.storeFile(filename, input);
			long end = System.currentTimeMillis();
			logger.info("Stored file use " + (end - start) + " ms");

			input.close();
			ftp.logout();
			logger.info("Logouted ftp");
			success = true;
		} catch (IOException e) {
			logger.error("上传IO异常", e);
			throw new Exception("上传IO异常", e);
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
					logger.info("Disconnected ftp");
				} catch (IOException ioe) {
					logger.error("上传IO异常", ioe);
					throw new Exception("上传IO异常", ioe);
				}
			}
		}
		return success;
	}

	/**
	 * Description: 向FTP服务器上传文件
	 * 
	 * @param url
	 *            FTP服务器hostname
	 * @param port
	 *            FTP服务器端口
	 * @param username
	 *            FTP登录账号
	 * @param password
	 *            FTP登录密码
	 * @param path
	 *            FTP服务器保存目录
	 * @param filename
	 *            要删除的文件名列表
	 * 
	 * @return 成功返回true，否则返回false
	 */
	public static boolean removeFile(String url, int port, String username,
			String password, String path, List<String> fileNameList,
			int connectTimeout, int dataTimeout) throws Exception {
		boolean success = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.setConnectTimeout(connectTimeout);

			ftp.connect(url, port);// 连接FTP服务器
			logger.debug("Connected FTP");

			// 如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
			ftp.login(username, password);// 登录
			logger.debug("Logined FTP");

			ftp.enterLocalPassiveMode();
			ftp.setDataTimeout(dataTimeout);

			// 上传zip等binary需设置如下类型
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

			reply = ftp.getReplyCode();
			logger.info("FTP replyCode is " + reply);

			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return success;
			}
			ftp.changeWorkingDirectory(path);
			logger.debug("Changed workingDirectory");

			long start = System.currentTimeMillis();
			
			for (String fileName : fileNameList) {
				ftp.deleteFile(fileName);
			}
			
			long end = System.currentTimeMillis();
			logger.info("Stored file use " + (end - start) + " ms");

			ftp.logout();
			logger.info("Logouted ftp");
			success = true;
		} catch (IOException e) {
			logger.error("删除文件IO异常", e);
			throw new Exception("删除文件IO异常", e);
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
					logger.info("Disconnected ftp");
				} catch (IOException ioe) {
					logger.error("删除文件IO异常", ioe);
					throw new Exception("删除文件IO异常", ioe);
				}
			}
		}
		return success;
	}
	
	public static void main(String[] args) {
		System.out.println("d");
	}
}
